<%@ page import="java.net.URLEncoder" %>
<%@ page import="cn.myschool.util.StrUtil" %>
<%@ page import="cn.myschool.entity.Admin" %>
<%@ page import="cn.myschool.service.AdminService" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String ip = request.getRemoteHost();
    request.setCharacterEncoding("utf-8");
    if (session.getAttribute("USER_LOGIN") != null) {
        out.println("请勿重复登录<a href=\"welcome.jsp\">返回主页面</a>");
        return;
    }

    String loginname = StrUtil.toStr(request.getParameter("username"));
    String upass = StrUtil.toStr(request.getParameter("password"));
    out.println(AdminService.ME.cheackPassword(new Admin(loginname, upass)) != 0);

    Cookie[] cookies = request.getCookies();
    Cookie c = null;
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("count")) {
                c = cookies[i];
                break;
            }
        }
    }
    int count = 0;
    if (c != null) {
        count = Integer.parseInt(c.getValue());
        c.setValue(String.valueOf(count));
        c.setMaxAge(84600);
        response.addCookie(c);
        //out.print("这是您第"+count+"次访问失败！");
    } else {
        Cookie cookie = new Cookie("count", "0");
        cookie.setMaxAge(84600);
        response.addCookie(cookie);
        //out.print("您这是第1次访问失败");
    }
    if (count < 3) {
        if (AdminService.ME.cheackPassword(new Admin(loginname, upass)) != 0) {
            c.setValue("0");
            session.setAttribute("USER_LOGIN", loginname);
            session.setAttribute("upass", upass);
            response.sendRedirect("welcome.jsp");
        } else {
            request.setAttribute("msg", "您这是第"+(count-1)+"次访问失败！");
            request.getRequestDispatcher("denglu.jsp").forward(request, response);
        }
    } else {
        request.setAttribute("msg", "您这是第"+(count-1)+"次访问失败！");
        request.getRequestDispatcher("denglu.jsp").forward(request, response);
    }


%>
</body>
</html>
